import java.util.Scanner;

import db.DbConnect;


public class AdminServerTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
    	AdminServer as = new AdminServer();
		DbConnect.connect();
		
		//as.anulare_Zbor("4");
		//System.out.println(as.adaugare_Zbor("Bucuresti", "Munchen", 25, 8, 1, 50, "4"));
		
		System.out.println("Insert command [Cancel, Add, List, Quit]");
		
		Scanner s = new Scanner(System.in);
		boolean isrunning = true;
		
		while(isrunning){
			String command = s.next();
			if(command.equalsIgnoreCase("quit"))
				return;
			else
			if(command.equalsIgnoreCase("cancel")){
				System.out.println("Insert flight id");
				String flightid = s.next();
				if(as.checkFlighID(flightid)){
					System.out.println(as.anulare_Zbor(flightid));
				}else{
					System.out.println("[cancel flight] Invalid flight ID"+flightid);
					System.out.println("[cancel flight]List all available flights? Y/N");
					String islist = s.next();
					while(!(islist.equalsIgnoreCase("y")||islist.equalsIgnoreCase("n"))){
						System.out.println("[cancel flight] List all available flights? Y/N [Answer with Y or N]");
						islist = s.next();
					}
					if(islist.equalsIgnoreCase("y")){
						as.listFlights("\t\t");
					}
					
				}
				
			}else
			if(command.equalsIgnoreCase("Add")){
				//System.out.println("[add flight]Insert flight ID");
				boolean tryid = true;
				while(tryid){
					System.out.println("[add flight]Insert flight ID");
					String flightid = s.next();
					tryid = false;
					if(!as.checkFlighID(flightid)){

						System.out.println("[add flight] Source name:");
						String source =  s.next();
						System.out.println("[add flight] Destination name:");
						String destination =  s.next();
						System.out.println("[add flight] Date [1-365]:");
						int date =  s.nextInt();
						while(date<1||date>365){
							System.out.println("[add flight] Invalid date [must be int 1-365]"+date);
							System.out.println(" Insert another date\n");
							date =  s.nextInt();
						}
						
						System.out.println("[add flight] Hour [0-23]:");	
						int hour =  s.nextInt();
						while(hour<0||hour>23){
							System.out.println("[add flight] Invalid hour [must be int 0-23]"+hour);
							System.out.println(" Insert another hour\n");
							hour =  s.nextInt();
						}
						System.out.println("[add flight] Duration [int >0]:");
						int duration =  s.nextInt();
						
						while(duration<0){
							System.out.println("[add flight] Invalid duration [must be int >0]"+duration);
							System.out.println(" Insert another duration\n");
							hour =  s.nextInt();
						}
						System.out.println("[add flight] Airplane capacity [int >0]:");
						int cap =  s.nextInt();
						while(duration<0){
							System.out.println("[add flight] Invalid airplane capacity [must be int >0]"+cap);
							System.out.println(" Insert another duration\n");
							cap =  s.nextInt();
						}
						System.out.println(as.adaugare_Zbor(source, destination, date, hour, duration, cap, flightid));
						
						
					}else{
						System.out.println("[add flight] Flight id already exists "+flightid);
						System.out.println("[add flight] Try another flight id or list available flights? Y/N/LIST");
						String isnewid = s.next();
						while(!(isnewid.equalsIgnoreCase("y")||
								isnewid.equalsIgnoreCase("n")||
								isnewid.equalsIgnoreCase("list"))){
							System.out.println("[add flight] Try another flight id or list available flights? Y/N/LIST\n [Answer with Y or N or LIST]");
							isnewid = s.next();
						}
						if(isnewid.equalsIgnoreCase("y")){
							tryid = true;
						}else
						if(isnewid.equalsIgnoreCase("list")){
							as.listFlights("\t\t");
						}
					}
				}
					
			}
			else if(command.equalsIgnoreCase("list")){
				as.listFlights("\t\t");
			}else
				System.out.println("Invalid command '"+command+"'");
		}
		
	}

}
